;
(function($){
    $.FormUtils = $.FormUtils || {};
    $.extend($.FormUtils, {
    
        formDataKey: "formData",
        /**
         * 对表单设置数据
         * @param {Object} formSelect
         * @param {Object} data
         */
        setFormData: function(formSelect, data){
            var form, $forms = $(formSelect);
            if ($forms.length > 0) {
                form = $forms[0];
            }
            
            var formData = $.data(form, $.FormUtils.formDataKey);
            $.data(form, $.FormUtils.formDataKey, $.extend({}, formData, data));
        },
        /**
         * 获取表单数据
         * @param {Object} formSelect
         */
        getFormData: function(formSelect){
            var form, $forms = $(formSelect);
            if ($forms.length > 0) {
                form = $forms[0];
            }
            var formData = $.data(form, $.FormUtils.formDataKey);
            return $.isEmptyObject(formData) ? {} : formData;
        },
        /**
         * 获取正常的表单数据，不转对象数据，纯字符串数据
         * @param {Object} formSelector
         */
        getFormNormalDataBySelector: function(formSelector){
            var fields = $(formSelector).find("input:text,input:hidden,input:password,select,textarea");
            
            $(formSelector).find("input:checkbox").each(function(ind, elm){
                fields.push($(elm));
            })
            
            $(formSelector).find("input:radio:checked").each(function(ind, elm){
                fields.push($(elm));
            })
            
            return $.FormUtils.getFormNormalDataByFields(fields);
        },
		
        /**
         * 获取正常的表单数据，不转对象数据，纯字符串数据
         * @param {Object} fields
         */
        getFormNormalDataByFields: function(fields){
            var formNormalData = {};
            for (var i = 0; i < fields.length; i++) {
                var $elm = $(fields[i]);
                if ($.StringUtils.isEmpty($elm.attr("name"))) 
                    continue;
                
                if ($elm.is("radio")) {
                    if ($elm.attr("checked") && ($elm.attr("checked") === true || $elm.attr("checked") == "true" || $elm.attr("checked") == "checked")) {
                        formNormalData[$elm.attr("name")] = $elm.val();
                    }
                }
                if ($elm.is("checkbox")) {
                    //checked 当复选框用作单选作用时,勾选复选框是value应为true/1/yes或其它,反选复选框时以为false/0/no.
                    //<k:checkbox formatRules="{checkedValueMap:{checked:true,unChecked:false}}"/>,
                    //<k:checkbox formatRules="{checkedValueMap:{checked:1,unChecked:0}}"/>,
                    //<k:checkbox formatRules="{checkedValueMap:{checked:yes,unChecked:no}}"/>,
                    //和jq-format-impl.js checkedValueMap 实现方法对应
                    if (!$.StringUtils.isEmpty($elm.attr("formatRules")) && $.parseJSON($elm.attr("formatRules")).checkedValueMap) {
                        var checkedValueMap = $.parseJSON($elm.attr("formatRules")).checkedValueMap;
                        formNormalData[$elm.attr("name")] = checkedValueMap[$elm.attr("checked")];
                    }
                    else {
                        //原生checkbox
                        formNormalData[$elm.attr("name")] = $elm.val();
                    }
                }
                else {
                    formNormalData[$elm.attr("name")] = $elm.val();
                }
                //alert($elm.attr("name")+" , "+$elm.val().length+" , "+$elm.val())
            }
            return formNormalData;
        },
        
        /**
         * 重置表单
         * @param {Object} selector
         */
        resetForm: function(selector){
            $(selector).trigger("reset");
            
            //checked 当复选框用作单选作用时,勾选复选框是value应为true/1/yes或其它,反选复选框时以为false/0/no.
            //<k:checkbox formatRules="{checkedValueMap:{checked:true,unChecked:false}}"/>,
            //<k:checkbox formatRules="{checkedValueMap:{checked:1,unChecked:0}}"/>,
            //<k:checkbox formatRules="{checkedValueMap:{checked:yes,unChecked:no}}"/>,
            //和jq-format-impl.js checkedValueMap 实现方法对应
            var checkboxFields = $("input:checkbox[formatRules]", selector);
            for (var i = 0; i < checkboxFields.length; i++) {
                var $checkboxFields = $(checkboxFields[i]);
                var formatRules = $.parseJSON($checkboxFields.attr("formatRules"));
                if (formatRules.checkedValueMap) {
                    if ($('#' + $checkboxFields.attr("id") + ':checked').attr("checked")) {
                        $checkboxFields.val(formatRules.checkedValueMap.checked)
                    }
                    else {
                        $checkboxFields.val(formatRules.checkedValueMap.unChecked)
                    }
                }
            }
        }
    });
})(jQuery);
